package com.jiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.entity.Log;
import com.jiyun.entity.LogDTO;
import com.jiyun.entity.User;
import com.jiyun.mapper.LogMapper;
import com.jiyun.mapper.UserMapper;
import com.jiyun.service.ILogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-03-10
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements ILogService {

    @Autowired
    LogMapper logMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public List<LogDTO> findLogById(Integer tid) {
        // select * from log where tid=?
        LambdaQueryWrapper<Log> qw = new LambdaQueryWrapper<>();
        qw.eq(Log::getTid,tid);

        List<Log> logs = logMapper.selectList(qw);
        List<LogDTO> list = new ArrayList<>();
        for (Log log : logs) {
            Integer uid = log.getUid();
            User user = userMapper.selectById(uid);
            LogDTO logDTO = new LogDTO();
            BeanUtils.copyProperties(log,logDTO);
            logDTO.setUser(user);
            list.add(logDTO);
        }
        return list;
    }
}
